
package com.bear.cmcc.NetTestHTTP.ui.adapter;

import com.bear.cmcc.NetTestHTTP.NetworkTestApp;
import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.component.CountrySortBean;
import com.bear.cmcc.NetTestHTTP.util.MyCommonUtil;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

public class NetSortAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater;

    private boolean isOpenSelect = true;

    private List<CountrySortBean> list;

    private ViewHolder viewholder;

    private int showColor = 0;

    public NetSortAdapter(Context context, List<CountrySortBean> list) {
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
        showColor = MyCommonUtil.getHigthShowColor(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        CountrySortBean csb = list.get(position);
        viewholder = null;
        view = null;
        if (view == null) {
            viewholder = new ViewHolder();
            view = layoutInflater.inflate(R.layout.item_national_sort, null);
            viewholder.lyItem = (LinearLayout) view.findViewById(R.id.lyItem);
            viewholder.cbSelect = (CheckBox) view.findViewById(R.id.cbSelect);
            viewholder.cbSelect.setClickable(false);
            viewholder.seqNumTV = (TextView) view.findViewById(R.id.seqNumTV);
            viewholder.provinceTV = (TextView) view.findViewById(R.id.provinceTV);
            viewholder.tdTV = (TextView) view.findViewById(R.id.tdTV);
            viewholder.gprsTV = (TextView) view.findViewById(R.id.gprsTV);
            viewholder.gprsSortTV = (TextView) view.findViewById(R.id.gprsSortTV);
            view.setTag(viewholder);
        } else {
            viewholder = (ViewHolder) view.getTag();
        }
        view.setMinimumHeight(60);
        if(csb.isSelect()){
        	viewholder.cbSelect.setChecked(true);
        }else{
        	viewholder.cbSelect.setChecked(false);
        }
        if (isOpenSelect) {
            viewholder.cbSelect.setVisibility(View.VISIBLE);
            viewholder.gprsSortTV.setVisibility(View.GONE);
        } else {
            viewholder.cbSelect.setVisibility(View.GONE);
            viewholder.gprsSortTV.setVisibility(View.VISIBLE);
        }
        viewholder.seqNumTV.setText(String.valueOf(position + 1));
        viewholder.provinceTV.setText(csb.getProvince_name());
        viewholder.tdTV.setText(String.valueOf(csb.getTd_downSpeed()));
        viewholder.gprsTV.setText(String.valueOf(csb.getGprs_downSpeed()));
        viewholder.lyItem.setMinimumHeight(60);
        viewholder.lyItem.setGravity(Gravity.CENTER);
        if (csb.isCurrent()) {
            view.setBackgroundColor(showColor);
        } else {
            view.setBackgroundColor(Color.TRANSPARENT);
            if (position % 2 == 0) {
                view.setBackgroundResource(R.drawable.backgroud);
            } else {
                view.setBackgroundResource(R.drawable.black_background);
            }
        }

        return view;
    }

    class ViewHolder {
        LinearLayout lyItem;

        CheckBox cbSelect;

        TextView seqNumTV;

        TextView provinceTV;

        TextView tdTV;

        TextView gprsTV;

        TextView gprsSortTV;
    }

    /**
     * 是否打开选择
     * 
     * @param isbool true打开，false关闭
     */
    public void setSelect(boolean isbool) {
        isOpenSelect = isbool;
        super.notifyDataSetChanged();
    }

    /**
     * 是否全选
     * 
     * @param isbool true选中，false取消
     */
    public void setAllSelect(boolean isbool) {
        for (int i = 0; i < list.size(); i++) {
        	
            list.get(i).setSelect(isbool);
        }
        super.notifyDataSetChanged();
    }
    /**
     * 
     * 设置当前省份为checked状态  分享数据用
     */
    public void setCurrentProvinceCheck(){
    	try {
            String province=NetworkTestApp.getApp().getProviceName();
            if(province!=null)
            {
             for (int i = 0; i < list.size(); i++) {
             	CountrySortBean counry=list.get(i);
                 if((counry.getProvince_name()).equals(province.substring(0, province.length()-1))){
                	 counry.setSelect(true);
                 }
             }
             super.notifyDataSetChanged();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 单选
     * 
     * @param location 单选下标
     */
    public void setRadioSelect(int location) {
        if (list.get(location).isSelect()) {
            list.get(location).setSelect(false);
        } else {
            list.get(location).setSelect(true);
        }
        super.notifyDataSetChanged();
    }

    /**
     * 是否选中
     * 
     * @param location 行号
     * @return true选中，false未选
     */
    public boolean isSelected(int location) {
        return list.get(location).isSelect();
    }

    /**
     * 设置高亮显示的省份。
     * 
     * @param proviceName 省份名称
     */
    public int setSelectShowItemt(String proviceName) {
        int selectShowItem = -1;
        if (proviceName != null && list != null) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setCurrent(false);
            }
            for (int i = 0; i < list.size(); i++) {
                if (proviceName.contains(list.get(i).getProvince_name())) {
                    list.get(i).setCurrent(true);
                    selectShowItem = i;
                    break;
                }
            }
        }
        return selectShowItem;

    }
}
